<?php
namespace PublicNo\Controller;
use Common\Controller\CommonController;
/**
 * 公众号平台
 */
class PublicNoPlatformController extends CommonController {
	protected $_public_no;
	
	protected function _initialize(){
		parent::_initialize();
		$this->_mycache = S(C('REDIS_DSN'));
		
		$this->_moduleid = 110;
		$this->_cust = new \Common\Model\CustModel();
		$this->_public_no = new \PublicNo\Model\PublicNoModel();
	}
	
	
	/**
	*获得签名
	* 
	*/
	public function getSignAction(){
		$username = I("request.username");
		$time = I("request.time");
		$this->data = $this->_getSign($username, $time);
		$this->outPut();
	}
	
	
    /**
     * 注册公众号用户
     */
    public function registerAction(){
    	$username = I('request.username');
    	$password = I('request.password');
    	$vercode = I('request.vercode');
    	$time = I('request.time');
    	$sign = I('request.sign');
    	$this->_register($username , $password , $vercode , $time , $sign);
    }
    
    private function _register($username , $password , $vercode , $time , $sign){
    	if(empty($username) || empty($password) || empty($vercode) || empty($time) || empty($sign)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	 
    	$this->_checkVerCode($username , $vercode , 3);//图形验证码检查
    	
    	$this->_checkUserExist($username,$time,$sign,2);//检查邮箱
    	 
    	$password = \tools::encodePwd($password);
    	 
    	$arr = array();
    	$arr['email'] = $username;//邮箱注册
    	$arr['password'] = $password;
    	$arr['isonline'] = 1;
    	$arr['logintimes'] = 1;
    	$arr['lastloginip'] = \tools::getClientIp();
    	$arr['lastlogintime'] = date("Y-m-d H:i:s");
    	$arr['auth'] = \tools::getSign($username . "_" . time() . "_" . $arr['lastloginip']);
    	$arr['webauth'] = $arr['auth'];
    	$arr['status'] = 0;
    	$arr['custtype'] = 3;
    	$arr['idenum'] = $this->_getIdeNum();
    	$custid = $this->_cust->addCust($arr);
    	if($custid){
    		$this->_delVerCode($username , 3);
    		
    		$custExtInfo['custid'] = $custid;
    		if(empty($this->_cust_ext)) $this->_cust_ext = new \Common\Model\CustExtModel();
    		$this->_cust_ext->saveCustExt($custExtInfo);
    		
    		$publicno = array();
    		$publicno['custid'] = $custid;
    		$publicnoid = $this->_public_no->addPublicNo($publicno);
    		if($publicnoid){
    			$resultData['publicnoid'] = $publicnoid;
    			$resultData['custid'] = $custid;
    			$resultData['email'] = $arr['email'];
    			$resultData['custtype'] = $arr['custtype'];
    			$this->_setCustInfoCache($resultData);
    			
    			$data = array();
    			$data['custid'] = $custid;
    			$data['expire'] = time()+C('TOKEN_EXPIRE');
    			$resultData['token'] = \aes::encrypt(json_encode($data), C('TOKEN_SALT'));
    			
    			$this->data = $resultData;
    			$this->outPut();
    		}else{
    			$this->status = 1910;
    			$this->msg = L('STATUS_1910');
    			$this->outPut();
    		}
    	}else{
    		$this->status = 1910;
    		$this->msg = L('STATUS_1910');
    		$this->outPut();
    	}
    }
    
    
    /**
     * 公众号平台登录
     */
    public function loginAction(){
    	$username = I('request.username');
    	$password = I('request.password');
    	$vercode = I('request.vercode');
    	$time = I('request.time');
    	$sign = I('request.sign');
    	$this->_login($username , $password , $vercode , $time , $sign);
    }
    
    private function _login($username , $password , $vercode , $time , $sign){
        if(empty($username) || empty($password) || empty($vercode) || empty($time) || empty($sign)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}else{
    		$this->_checkSign($username,$time,$sign);
    		
    		$this->_checkVerCode($username , $vercode , 3);
    		 
    		//判断是否被暂时禁止，防止暴力登录
    		$failureTimesKey = \cachekey::$key['BASE_KEY_CONF']['FAILURE_LOGIN_TIMES'].$username;
    		$failureTimes = (int)$this->_mycache->get($failureTimesKey);
    		if($failureTimes > (int)C('PER_DAY_MAX_ERROR_LOGIN_TIMES')){//被锁住，暂时不能登录
    			$this->status = 1911;
    			$this->msg = L('STATUS_1911');
    			$this->outPut();
    		}
    		 
    		$publicNoInfo = $this->_public_no->getPublicNoInfoByEmail($username);
    		if(empty($publicNoInfo)){
    			$this->status = 1913;
    			$this->msg = L('STATUS_1913');
    			$this->outPut();
    		}elseif(!\tools::verifyPwd($password , $publicNoInfo['password'])){
    			$this->_mycache->set($failureTimesKey , $failureTimes+1);//密码错误登录次数累加1
    			$this->_mycache->expire($failureTimesKey , C('FORBID_LOGIN_EXPIRE'));
    			
    			$this->status = 1909;
    			$this->msg = L('STATUS_1909');
    			$this->outPut();
    		}
    		 
    		$arr['custid'] = $publicNoInfo['custid'];
    		$arr['email'] = $publicNoInfo['email'];
    		$arr['custtype'] = $publicNoInfo['custtype'];
    		$arr['nickname'] = $publicNoInfo['nickname'];
    		$arr['realname'] = $publicNoInfo['realname'];
    		$arr['pinyinname'] = $publicNoInfo['pinyinname'];
    		$arr['publicnoid'] = $publicNoInfo['publicnoid'];
    		$this->_setCustInfoCache($arr); 
    		
    		$data = array();
    		$data['custid'] = $publicNoInfo['custid'];
    		$data['expire'] = time()+C('TOKEN_EXPIRE');
    		$publicNoInfo['token'] = \aes::encrypt(json_encode($data), C('TOKEN_SALT'));
    		
    		$this->data = $publicNoInfo;
    		$this->outPut();
    	}
    }
    
    
    /**
     * 公众号平台登出
     */
    public function logoutAction(){
		$_SESSION = null;
		@setCookie(session_name(),'',time()-3600,'/'); 
		@session_destroy();
    }
    
    

    
        
    /**
     * 发送图形验证码（注册公众号用户时使用）
     */
    public function sendImgCodeAction(){
    	$username = I('request.username');
    	$time = I('request.time');
    	$sign = I('request.sign');
    	$this->_sendImgCode($username , $time , $sign);
    }
    
    private function _sendImgCode($username , $time , $sign){
    	if(empty($username)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	$this->_checkSign($username,$time,$sign);
    
    	//随机生成验证码
    	$vercode = \tools::genRandomNum(4);
    
    	$verKey = \cachekey::$key['BASE_KEY_CONF']['VERIFY_IMG_CODE'].$username;
    	$this->_mycache->set($verKey , $vercode);//缓存验证码
    	$this->_mycache->expire($verKey , C('VERCODE_EXPIRE'));
    	 
    	$Verify = new \Think\Verify();
    	$Verify->length   = 4;
    	$Verify->__set('fontSize',14);//字体大小
    	$Verify->__set('imageH',30);//图片高度
    	$Verify->__set('imageW',110);//图片宽度
    	$Verify->appEntry($vercode);
    }
    


    
    
    /**
     * 检查用户是否已经注册
     */
    public function checkUserExistAction(){
    	$username = I("request.username");
    	$time = I("request.time");
    	$sign = I("request.sign");
    	$this->_checkUserExist($username , $time , $sign , 2);//检查邮箱
    	$this->outPut();
    }
    
    
    /**
     * 检查用户对应的验证码
     */
    public function checkVerCodeAction(){
    	$username = I("request.username");
    	$vercode = I("request.vercode");
    	//$regtype = (int)I("request.regtype");
    	$this->_checkVerCode($username , $vercode , 3);
    	$this->outPut();
    }
    
    /**
     * 重置用户密码
     */
    public function resetPublicNoPwdAction(){
    	$username = I("request.username");
    	$password = I("request.password");
    	$vercode = I('request.vercode');
    	$time = I('request.time');
    	$sign = I('request.sign');
    	$this->_resetPublicNoPwd($username,$password,$vercode,$time,$sign);
    }
    
    private function _resetPublicNoPwd($username,$password,$vercode,$time,$sign){
        if(empty($username) || empty($password) || empty($vercode) || empty($time) || empty($sign)){
    		$this->status = 1000;
    		$this->msg = L('STATUS_1000');
    		$this->outPut();
    	}
    	
    	if(!\validate::isEmail($username)){
    		$this->status = 1917;
    		$this->msg = L('STATUS_1917');
    		$this->outPut();
    	}
    
    	$this->_checkSign($username,$time,$sign);
    	 
    	$this->_checkVerCode($username , $vercode , 2);
    
    	$custInfo = $this->_cust->getCustInfoByEmail($username);
    	if(empty($custInfo)){
    		$this->status = 1913;
    		$this->msg = L('STATUS_1913');
    		$this->outPut();
    	}
    
    	$arr = array();
    	$arr['custid'] = $custInfo['custid'];
    	$arr['password'] = \tools::encodePwd($password);
    	$ret = $this->_cust->updateCust($arr);
    	if($ret){
    		$this->_delVerCode($username , 2);
    		
    		$this->outPut();
    	}else{
    		$this->status = 1914;
    		$this->msg = L('STATUS_1914');
    		$this->outPut();
    	}
    }
    
  
}
